package JUC;

import java.util.concurrent.*;

public class FutureThreadPoolDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        //3个任务,只有一个线程main来处理
        long startTime=System.currentTimeMillis();
//        try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {throw new RuntimeException(e);}
//        try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {throw new RuntimeException(e);}
//        try {TimeUnit.SECONDS.sleep(10);} catch (InterruptedException e) {throw new RuntimeException(e);}
        FutureTask<String> futureTask1=new FutureTask<String>(()->{
            try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {throw new RuntimeException(e);}
            return "futureTask1";
        });
        FutureTask<String> futureTask2=new FutureTask<String>(()->{
            try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {throw new RuntimeException(e);}
            return "futureTask2";
        });
        FutureTask<String> futureTask3=new FutureTask<String>(()->{
            try {TimeUnit.SECONDS.sleep(10);} catch (InterruptedException e) {throw new RuntimeException(e);}
            return "futureTask3";
        });

        ExecutorService executorService= Executors.newFixedThreadPool(3);
        executorService.submit(futureTask1);
        executorService.submit(futureTask2);
        executorService.submit(futureTask3);

        executorService.shutdown();
        while (true){
            if (futureTask3.isDone()){
                System.out.println(futureTask3.get());
                break;
            }else {
                try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {throw new RuntimeException(e);}
                System.out.println("正在处理中");
            }
        }
        long endTime=System.currentTimeMillis();
        System.out.println("-------costTime:"+(endTime-startTime)+"毫秒");
        System.out.println(Thread.currentThread().getName()+"--end");
    }
}
